#! /bin/bash
if [ -z "$MYSQL_SERVICE_HOST" ];then
    echo "MYSQL_SERVICE_HOST is not defined"
    exit 1
fi

if [ -z "$DUBHE_ENVIRONMENT" ];then
    echo "DUBHE_ENVIRONMENT is not defined"
    exit 1
fi

result=`echo "show databases" | mysql -h $MYSQL_SERVICE_HOST -u root --ssl-mode=DISABLED -p$MYSQL_ROOT_PASSWORD | grep -x $DUBHE_ENVIRONMENT | wc -l`

if [ $result = "1" ];then
    echo "database $DUBHE_ENVIRONMENT is already defined"
    exit 0
fi

sed -i "s/DUBHE_ENVIRONMENT/$DUBHE_ENVIRONMENT/g" /data/sql/*

# TODO: change sql
set -e

cat /data/sql/00-Dubhe-DB.sql | mysql -h $MYSQL_SERVICE_HOST -u root --ssl-mode=DISABLED -p$MYSQL_ROOT_PASSWORD
cat /data/sql/01-Dubhe-DDL.sql | mysql -h $MYSQL_SERVICE_HOST -u root --ssl-mode=DISABLED -p$MYSQL_ROOT_PASSWORD
#cat /data/sql/02-Dubhe-DML.sql | mysql -h $MYSQL_SERVICE_HOST -u root --ssl-mode=DISABLED -p$MYSQL_ROOT_PASSWORD
#cat /data/sql/09-Dubhe-Patch.sql | mysql -h $MYSQL_SERVICE_HOST -u root --ssl-mode=DISABLED -p$MYSQL_ROOT_PASSWORD
# 3.0 更新sql
#cat /data/sql/11-Dubhe-Patch-3.0.sql | mysql -h $MYSQL_SERVICE_HOST -u root --ssl-mode=DISABLED -p$MYSQL_ROOT_PASSWORD

exit 0
